<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        class HashTable {
            constructor() {
                this.table = [];
            }

             // 更好的预处理方法(社区推荐)
             static djb2HashCode() {
                var hash = 5381;
                for (var i = 0; i < key.length; i++) {
                    hash = hash * 33 + key.charCodeAt(i);
                }
                return hash % 1013;
            };


            // 对载入数据的预处理
            static loseloseHashCode(key) {
                let hash = 0;
                for (let i = 0; i < key.length; i++) {
                    hash += key.charCodeAt(i);
                }
                return hash % 37;
            }

            // put 添加数据
            put(key, value) {
                let position = HashTable.loseloseHashCode(key);
                console.log(position+'-'+key);
                this.table[position] = value;
            }

            // get 获取数据
            get(key){
                return this.table[HashTable.loseloseHashCode(key)]
            }

            // remove 移除数据
            remove(key){
                let temp = this.get(key);
                this.table[HashTable.loseloseHashCode(key)] = undefined;
                return temp;
            }

            // print 输出数据
            print(){
                for (let i = 0; i < this.table.length; i++) {
                   if(this.table[i]){
                       console.log(i+':'+this.table[i]);
                   }
                }
            }
        }


        let table = new HashTable();
        table.put('haha', 10);
        table.put('pa', 20);
        table.put('rua', 30);
        table.print();
    </script>
</body>

</html>